package com.lhgray.sglhome.presentation.viewModel

import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.freightoa.foundation.viewmodel.BaseState
import com.freightoa.foundation.viewmodel.UiIntent
import com.lhgray.sglhome.domain.usecase.OrderListUseCase
import com.lhgray.sglhome.domain.usecase.PicUploadUseCase
import com.lhgray.sglhome.domain.model.SourceData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 上传图片的页面
 */
sealed class PicUpStates {
    data class Loading(val isLoading: Boolean) : PicUpStates()
    data class Error(val errorMessage: Int) : PicUpStates()
    data class Success(val data: PicUploadState) : PicUpStates()
    data object Ide : PicUpStates()
}

data class PicUploadState(
    val sourceData: SourceData? = null,
    val isLoading: Boolean = false,
    val errorMsg: String? = null,
) : BaseState


@HiltViewModel
class PicUploadViewModel @Inject constructor(
    private val picUploadUseCase: PicUploadUseCase,
    private val orderListUseCase: OrderListUseCase,
) : ViewModel() {

    private val _sourceData: MutableStateFlow<SourceData> = MutableStateFlow(SourceData())
    private val _loading: MutableStateFlow<Boolean> = MutableStateFlow(false)
    private val _errorMsg: MutableStateFlow<Throwable?> = MutableStateFlow(null)
    private val _picUpState: MutableStateFlow<PicUpStates> = MutableStateFlow(PicUpStates.Ide)
    val uiState: StateFlow<PicUploadState> = combine(
        _sourceData, _loading, _errorMsg, _picUpState
    ) { sourceData, loading, errorMsg, picUpState ->
        when (picUpState) {
            is PicUpStates.Error -> {
                PicUploadState(sourceData, false, errorMsg?.message)
            }

            is PicUpStates.Loading -> {
                PicUploadState(sourceData, true, null)
            }

            is PicUpStates.Success -> {
                PicUploadState(sourceData, false, null)
            }

            PicUpStates.Ide -> PicUploadState()

        }

    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = PicUploadState(SourceData(), isLoading = false)
    )

    /**
     * 根据输入框的数据搜索列表信息的
     */
    fun onSearchNumber(fileNo: String) {
        if (fileNo.isNotEmpty()) {
            getOrderList(fileNo)
        }
    }

    /**
     * 获取订单列表信息
     */
    private fun getOrderList(fileNo: String) {
        _picUpState.value = PicUpStates.Loading(true)
        viewModelScope.launch {
            val result = orderListUseCase.invoke(fileNo)
            if (result.isSuccess) {
                _sourceData.value = result.getOrDefault(SourceData())!!
                _picUpState.value = PicUpStates.Success(PicUploadState())
                //
            } else {
                _errorMsg.value = result.exceptionOrNull()
                _picUpState.value = PicUpStates.Error(0)
            }
        }
    }

    fun handleIntent(intent: PicUploadIntent) {

    }

}

sealed interface PicUploadIntent : UiIntent {
    data class PicUpload(val list: MutableList<Uri>) : PicUploadIntent
}